<?php

/**
 * 会员注册
 */

namespace app\member\api;

use \app\base\api\BaseApi;

class RegisterApi extends BaseApi {

    /**
     * 注册用户
     */
    public function index() {
        $loginData = target('member/Member', 'service')->regUser($this->data['username'], $this->data['password'], $this->data['code']);
        if(!$loginData) {
            $this->error(target('Member/Member', 'service')->getError());
        }
        $this->success('帐号注册成功!', $loginData);
    }

    /**
     * 找回密码
     */
    public function forgot() {

        $userName = $this->data['username'];
        $password = $this->data['password'];
        $password2 = $this->data['password2'];
        $code = $this->data['code'];
        if (empty($userName) || empty($password)) {
            $this->error('用户名或密码未填写！');
        }
        if ($password <> $password2) {
            $this->error('重复密码不正确!');
        }
        $loginData = target('member/Member', 'service')->forgotUser($userName, $password, $code);
        if (!$loginData) {
            $this->error(target('member/Member', 'service')->getError());
        }
        $this->success('密码修改成功!');
    }

    /**
     * 注册协议
     */
    public function agreement() {
        $config = target('member/MemberConfig')->getConfig();
        $this->success('ok', html_out($config['reg_agreement']));
    }

    /**
     * 图片验证码
     */
    protected function getImgCode() {
        return new \dux\lib\Vcode(90, 37, 4, '', 'code');
    }

    /**
     * 获取验证码
     */
    public function getCode() {
/*        if (!$this->getImgCode()->check($this->data['imgcode'])) {
            $this->error('图片验证码输入不正确!');
        }*/
        if (!target('member/Member', 'service')->getVerify($this->data['username'])) {
            $this->error(target('member/Member', 'service')->getError());
        }
        $this->success('验证码已发送,请注意查收!');
    }

}